package com.yanqu.road.web.controller.usercenter;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.admin.AdminResult;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.http.HttpHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/usercenter/admin")
public class CenterHelperAdminController {


    /**
     *  21.1 调整服务器时间
     */
    @ResponseBody
    @RequestMapping(value = "modifyServerDate", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String modifyServerDate(HttpServletRequest request, @RequestBody JSONObject jsonObject) {


        int channelId = jsonObject.getIntValue("channelId");
        long serverId = jsonObject.getLongValue("serverId");
        long activityServerId = jsonObject.getLongValue("activityServerId");
        String modifyTime = jsonObject.getString("modifyTime");

        ServerInfo serverInfo = ServerListMgr.getServerInfo(serverId);
        if(serverInfo == null){
            return AdminResult.failMsg("参数无效").toJsonString();
        }

        ServerInfo activityServerInfo = ServerListMgr.getServerInfo(activityServerId);
        if(activityServerInfo == null){
            return AdminResult.failMsg("参数无效").toJsonString();
        }

        Calendar instance = Calendar.getInstance();
        instance.setTime(DateHelper.getString2Date(modifyTime));
        JSONObject param = new JSONObject();

        param.put("year", instance.get(Calendar.YEAR));
        param.put("month", instance.get(Calendar.MONTH) + 1);
        param.put("day", instance.get(Calendar.DATE));
        param.put("hour", instance.get(Calendar.HOUR_OF_DAY));
        param.put("minute", instance.get(Calendar.MINUTE));
        param.put("second", instance.get(Calendar.SECOND));
        param.put("serverId", serverId);

        String op = "AgentSystem|resetTime";
        JSONObject callParam = new JSONObject();
        callParam.put("op", op);
        callParam.put("param",param);
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("param", callParam.toJSONString());

        String response = HttpHelper.doPost(activityServerInfo.getWebServerUrl() + "/adminOperate", paramMap, "UTF-8");
        if(!StringUtils.isNullOrEmpty(response)){
            return response;
        }

        return AdminResult.defaultInstance().toJsonString();
    }
}
